import { _decorator, Component, instantiate, UITransform ,Node, Canvas, Layers} from 'cc';
import { ResManager } from '../res/ResManager';
import { G_VIEW_SIZE } from '../../../../Boost';
import { EViewLayer } from './EViewLayer';
import { Match3UI } from '../../../../Match3/Script/Match3UI';
import { BL } from '../res/ResConst';
const { ccclass, property } = _decorator;


function getUIClassBUrl(uiClass: any): IBundleUrl {
    if (uiClass === Match3UI) {
        return BL("Match3UI", "Match3BN")
    }
    console.log(`其它类型暂未处理`)
}

class MyLayer {
    public readonly node: Node;
    constructor(
        public readonly layer: EViewLayer,
        public readonly canvas: Canvas,
        name: string,
    ) {
        const node = this.node = new Node(name);
        node.layer = Layers.Enum.UI_2D;
        node.addComponent(UITransform);
        canvas.node.addChild(node);
    }
}


export class UIManager{

    private static _instance: UIManager = null!;

    static getInstance(){
        if (this._instance === null) {
            this._instance = new UIManager();
        }
        return this._instance;
    }

    private constructor(){

    }

    open(uiClass: any) {
        const bUrl = getUIClassBUrl(uiClass);
        // ResManager.getInstance().loadPrefabByBUrl("Match3BN", "Match3UI", prefab => {
        ResManager.getInstance().loadPrefabByBUrl(bUrl, prefab => {
            let match3Node = instantiate(prefab);
            // this.node.addChild(match3Node);
            this.m_Layers[EViewLayer.UI].node.addChild(match3Node);
            match3Node.getComponent(UITransform).setContentSize(G_VIEW_SIZE.clone());
        });
    }

    private m_Canvas:Canvas = null;
    private m_Layers: MyLayer[] = []

    init(canvas: Canvas) {
        this.m_Canvas = canvas;
        for (let layer = EViewLayer.Scene, maxLayer = EViewLayer.Toast; layer <= maxLayer; ++layer) {
            this.m_Layers.push(new MyLayer(layer, canvas, EViewLayer[layer]));
        }
    }
}
